package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.ProjectCollectOriginal.ProjectCollectContent;

public class DetailProjectCollectFragment extends Fragment {
  public static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.DetailProjectCollectFragment.EXTRA_CONTENT";

  private TextView mTvprojectKind;
  private TextView mTvprojectName;
  private TextView mTvstages;
  private TextView mTvcustomer;
  private TextView mTvsecondPart;
  private TextView mTvprojectLeaveDays;
  private TextView mTvfinishTime;
  private TextView mTvpmName;
  private TextView mTvprojectDept;
  private TextView mTvprojectJoiner;
  private TextView mTvsignTime;//签订日期
  private TextView mTvregisteredName;
  private TextView mTvregisteredTime;
  private TextView mTvnotes;

  private ProjectCollectContent content;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.detail_project);
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    content = (ProjectCollectContent) getArguments().getSerializable(EXTRA_CONTENT);
    super.onCreate(savedInstanceState);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_detail_project, container, false);
    initData(v);

    return v;
  }

  private void initData(View v) {
    mTvprojectKind = (TextView) v.findViewById(R.id.tv_projectKind);
    mTvprojectName = (TextView) v.findViewById(R.id.tv_projectName);
    mTvstages = (TextView) v.findViewById(R.id.tv_stages);
    mTvcustomer = (TextView) v.findViewById(R.id.tv_customer);
    mTvsecondPart = (TextView) v.findViewById(R.id.tv_secondParty);
    mTvprojectLeaveDays = (TextView) v.findViewById(R.id.tv_projectLeaveDays);
    mTvfinishTime = (TextView) v.findViewById(R.id.tv_finishTime);
    mTvsignTime = (TextView) v.findViewById(R.id.tv_signTime);
    mTvpmName = (TextView) v.findViewById(R.id.tv_pmName);
    mTvprojectDept = (TextView) v.findViewById(R.id.tv_projectDept);
    mTvprojectJoiner = (TextView) v.findViewById(R.id.tv_project_joiner);
    mTvregisteredName = (TextView) v.findViewById(R.id.tv_registeredName);
    mTvregisteredTime = (TextView) v.findViewById(R.id.tv_registeredTime);
    mTvnotes = (TextView) v.findViewById(R.id.tv_notes);

    mTvprojectKind.setText(content.getProjectKindName());
    mTvprojectName.setText(content.getProjectName());
    mTvstages.setText(content.getStages());
    mTvcustomer.setText(content.getPartaName());
    mTvsecondPart.setText(content.getPartbDeptName());

    if ("T".equals(content.getIfAcceptance())) {
      mTvprojectLeaveDays.setText("已验收");
    } else {
      if (!TextUtils.isEmpty(content.getProjectLeaveDays())) {
        if (Integer.parseInt(content.getProjectLeaveDays()) < 0) {
          mTvprojectLeaveDays.setText("已到期");
        } else {
          mTvprojectLeaveDays.setText(content.getProjectLeaveDays());
        }
      }
    }
    mTvfinishTime.setText(content.getRequireDate());
    mTvsignTime.setText(content.getProjectSignDate());
    mTvpmName.setText(content.getPmName());
    mTvprojectDept.setText(content.getPmDeptName());
    mTvprojectJoiner.setText(content.getPrincipalNames());
    mTvregisteredName.setText(content.getRegStaffName());
    mTvregisteredTime.setText(content.getRegDate());
    mTvnotes.setText(content.getRemark());
  }

  public static DetailProjectCollectFragment newInstance(ProjectCollectContent item) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, item);
    DetailProjectCollectFragment fragment = new DetailProjectCollectFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
